﻿Imports System.Drawing.Printing

Public Class DosPrint

    Public Shared anchoColumnasImp() As Integer

    Private Shared prtDoc As PrintDocument
    Private Shared prtFont As System.Drawing.Font
    Private Shared text As String
    Private Shared prtSettings As PrinterSettings

    Public Shared lineaActual As Integer = 1
    Public Shared yPos As Single = 100

    Private Shared displayRectangle As New Rectangle(New Point(20, yPos), New Size(800, yPos))
    Private Shared format As New StringFormat(StringFormatFlags.NoClip)

    Public Shared Function WriteLn(ByRef text As String, _
                                   Optional ByRef alig As StringAlignment = StringAlignment.Near, _
                                   Optional ByRef tLetra As String = "Arial", _
                                   Optional ByRef tamano As Integer = 11, _
                                   Optional ByRef estiloFondo As FontStyle = FontStyle.Regular) As Single

        displayRectangle = New Rectangle(New Point(20, yPos), New Size(800, yPos))
        ' Set the LineAlignment and Alignment properties for 
        ' both StringFormat objects to different values.
        format.LineAlignment = StringAlignment.Near
        format.Alignment = alig

        prtFont = New System.Drawing.Font(tLetra, tamano, estiloFondo)

        If prtDoc Is Nothing Then
            prtDoc = New System.Drawing.Printing.PrintDocument
            AddHandler prtDoc.PrintPage, AddressOf prt_PrintPage
        End If
        prtDoc.PrinterSettings = prtSettings

        DosPrint.text = text
        prtDoc.Print()

        Return lineaActual
    End Function

    Private Shared Sub prt_PrintPage(ByVal sender As Object, _
                             ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        ' Este evento se produce cada vez que se va a imprimir una página
        Dim lineHeight As Single
        'Dim leftMargin As Single = e.MarginBounds.Left       

        e.Graphics.Clear(System.Drawing.Color.White)
        lineHeight = prtFont.GetHeight(e.Graphics)

        ' imprimir cada una de las líneas de esta página
        ' Do
        e.Graphics.DrawString(text, prtFont, Brushes.Black, RectangleF.op_Implicit(displayRectangle), format)
        yPos += lineHeight
        lineaActual += 1

        'Loop Until yPos >= e.MarginBounds.Bottom - 40 OrElse lineaActual >= dtProd.Rows.Count ''''' menos dos renglones
        '
        If yPos >= e.MarginBounds.Bottom - 40 Then
            e.HasMorePages = True
            'yPos = 100
        End If

        'If lineaActual < dtProd.Rows.Count Then
        '    e.HasMorePages = True
        'Else
        '    e.HasMorePages = False
        'End If
    End Sub

    Public Shared Function seleccionarImpresora() As Boolean
        Dim prtDialog As New PrintDialog
        If prtSettings Is Nothing Then
            prtSettings = New PrinterSettings
        End If

        With prtDialog
            .AllowPrintToFile = False
            .AllowSelection = False
            .AllowSomePages = True
            prtSettings.MinimumPage = 1
            prtSettings.MaximumPage = 99
            prtSettings.FromPage = 1
            prtSettings.ToPage = 99
            '
            .PrintToFile = False
            .ShowHelp = False
            .ShowNetwork = True
            .PrinterSettings = prtSettings

            If .ShowDialog() = DialogResult.OK Then
                prtSettings = .PrinterSettings
            Else
                Return False
            End If

        End With

        Return True
    End Function

    Public Shared Sub ToColumnHeaders()
        ReDim anchoColumnasImp(6)
        anchoColumnasImp(0) = 10
        anchoColumnasImp(1) = 40
        anchoColumnasImp(2) = 8
        anchoColumnasImp(3) = 8
        anchoColumnasImp(4) = 8
        anchoColumnasImp(5) = 12
    End Sub

    Public Shared Function ajustar(ByVal cadena As String, ByVal ancho As Integer, _
                                                Optional ByRef alignRigth As Boolean = False) As String
        ' devuelve una cadena con el ancho indicado
        If cadena = Nothing OrElse cadena.Length = 0 Then
            Return New String(" "c, ancho)
        End If

        If alignRigth = True Then
            If ancho > cadena.Length Then cadena = New String(" "c, ancho - cadena.Length) & cadena
            Return cadena.Substring(cadena.Length - ancho, ancho)
        Else
            If ancho > cadena.Length Then cadena &= New String(" "c, ancho - cadena.Length)
            Return cadena.Substring(0, ancho)
        End If

        'If ancho > cadena.Length Then
        '    cadena &= New String(" "c, ancho - cadena.Length)
        'End If

        Return cadena.Substring(0, ancho)

    End Function

End Class
